package com.ocean.admin.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocean.admin.entity.SysUserRole;
import com.ocean.admin.mapper.SysUserRoleMapper;
import com.ocean.admin.service.ISysUserRoleService;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户和角色关联表 服务实现类
 * </p>
 *
 * @author zhang
 * @since 2022-08-16
 */
@Service
public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleMapper, SysUserRole> implements ISysUserRoleService {

    /**
     * 新增用户和角色关系
     *
     * @param roleIds 角色ID组
     * @param userId  用户ID
     */
    @Override
    public void saveUserRole(Long[] roleIds, Long userId) {
        if (roleIds != null && roleIds.length > 0) {
            List<SysUserRole> collect = Arrays.stream(roleIds).map(res -> {
                SysUserRole ur = new SysUserRole();
                ur.setUserId(userId);
                ur.setRoleId(res);
                return ur;
            }).collect(Collectors.toList());
            this.saveBatch(collect);
        }
    }
}
